
#include "xlx_config.h"
#include "xlx_configfile.h"
#include "xlx_core.h"
#include "xlx_version.h"

static const char* TAG = "CfgF";

#if 1
// 国内的

//默认服务器地址
const char *default_server_host = "gate.xiaolangxing.com";//"xlx.0x2000.com";
const int default_server_port = 3379;

//默认服务器地址
const char *default_tms_host = "tmsdev.xlxpro.com";
const int default_tms_port = 9527;

//测试环境
// const char *default_server_host = "device.xiaolangxing.com";
// const int default_server_port = 2379;
#else
//默认服务器地址
const char *default_server_host = "gate.xiaolangxing.com";//"xlx.0x2000.com";
const int default_server_port = 3379;

//默认服务器地址
const char *default_tms_host = "tmsdev.xlxpro.com";
const int default_tms_port = 9527;

//测试环境
// const char *default_server_host = "device.xiaolangxing.com";
// const int default_server_port = 2379;
#endif


/**
 *加载为默认配置
 */
void tutuconfig_load_default(void)
{        
    memset(&TUTUG(config), 0, sizeof(TUTUG(config)));
    
    g_tutuconfig.gps_inteval = 180;//默认15s定位一次,现在3分钟定位一次
    g_tutuconfig.snd_heart_time = 300;//秒,5分钟一次心跳
    g_tutuconfig.audio_mode.file_longtime = 15;//秒
    g_tutuconfig.audio_mode.mode = 0;//声控报警模式
    memcpy(g_tutuconfig.device_pwd,"123456",6);//默认设备密码

    g_tutuconfig.enable_status |= ENABLE_GPS;//默认开启gps功能
    g_tutuconfig.enable_status |= ENABLE_NET;//默认开启网络功能

    g_tutuconfig.shake_auto_open_gps = 1;
    g_tutuconfig.enable_status |= ENABLE_SHAKE;//默认开启震动功能
    g_tutuconfig.enable_status |= ENABLE_TURN;//默认开启拐点补传

    g_tutuconfig.audio_sensitivity = 8;//0-100
    g_tutuconfig.audio_sensitivity_hold_time = 180;//秒
    g_tutuconfig.shake_sensitivity = 20;//震动灵敏度

    g_tutuconfig.mode = 0;
    g_tutuconfig.audio_bitrate = 7;
    g_tutuconfig.low_voltage_threshold = 10;//电量低于10%

}

/**
 *保存配置文件
 */
void tutuconfig_save(void){
    g_tutufile.write_file(TUTU_CONFIG_FILE_NAME,(char*)&g_tutuconfig,sizeof(TutuConfig));
}

/**
 *加载配置文件
 */
void tutuconfig_load(void){
    const char * file = TUTU_CONFIG_FILE_NAME;
    unsigned long int len = sizeof(TutuConfig);
    
    TUTU_LOGI(TAG, "%s@line%d",__FUNCTION__,__LINE__);
    int fileLen = g_tutufile.get_file_size(file);
    TUTU_LOGI(TAG, "%s@line%d",__FUNCTION__,__LINE__);

    if (fileLen > 0) {
        unsigned long int size = fileLen <= len ? fileLen : len;  //以最小的数据量为准
        if(!g_tutufile.read_file(file, (char*)&g_tutuconfig, &size)) {    //读取失败，删除文件并重启
            g_tutufile.my_delfile(file);
            g_tutuos.restart();
        }
    } else {
        TUTU_LOGI(TAG, "%s@line%d",__FUNCTION__,__LINE__);
        tutuconfig_save();
    }
}

//保存连接服务器用的信息
void tutuconfig_save_servercfg(void){
    g_tutufile.write_file(TUTU_ADDR_FILE_NAME,(char*)&g_tutuservercfg,sizeof(TutuServerCfg));
}

//加载连接服务器用的信息
void tutuconfig_load_servercfg(void){
    unsigned long int len = sizeof(TutuServerCfg);
    
    const char * file = TUTU_ADDR_FILE_NAME;
    if(g_tutufile.get_file_size(file) == len){
        g_tutufile.read_file(file,(char*)&g_tutuservercfg,&len);
    }else{
        memset(&g_tutuservercfg,0,len);
    }

    //如果没有读取的配置文件，则使用默认的配置
    if(g_tutuservercfg.server_port < 1){
        g_tutuservercfg.server_port = default_server_port;//服务器端口号
        memcpy(g_tutuservercfg.server_host,default_server_host,strlen(default_server_host));//服务器地址
    }

    //如果没有读取的配置文件，则使用默认的配置
    if(g_tutuservercfg.tms_srv_port < 1){
        g_tutuservercfg.tms_srv_port = default_tms_port;// TMS服务器端口号
        memcpy(g_tutuservercfg.tms_srv_host,default_tms_host,strlen(default_tms_host));//服务器地址
    }    
}

//加载版本code
int tutuconfig_load_versioncode(void){
    int vcode = 0;
    const char * file = TUTU_VERSION_FILE_NAME;
    unsigned long int len = sizeof(int);

    if(g_tutufile.get_file_size(file) == len){
        g_tutufile.read_file(file,(char*)&vcode,&len);
    }

    return vcode;
}

//保存版本code到配置文件
void tutuconfig_save_versioncode(void){
    int vcode = get_version_code();
    int len = sizeof(int);
    g_tutufile.write_file(TUTU_VERSION_FILE_NAME,(char*)&vcode,len);
}

//删除掉所有配置文件
void tutuconfig_delall_file(void){
    g_tutufile.my_delfile(TUTU_CONFIG_FILE_NAME);
    g_tutufile.my_delfile(TUTU_ADDR_FILE_NAME);
    g_tutufile.my_delfile(TUTU_VERSION_FILE_NAME);
    g_tutufile.my_delfile(TUTU_GPS_FILE_NAME);
    g_tutufile.my_delfile(TUTU_DOMAIN_FILE_NAME);
    g_tutufile.my_delfile(TUTU_WAKEUP_SRC_FILE_NAME);
}

void TutuConfig_Init(void){
    int vcode = 0;
    TUTU_LOGI(TAG, "%s(%d)", __FUNCTION__, __LINE__); 
    vcode = tutuconfig_load_versioncode();
    if(vcode > 0){
        TUTU_LOGI(TAG, "%s:%d(vcode=%d/%d)",__FUNCTION__, __LINE__, vcode, get_version_code());            
        if(vcode != get_version_code()){
            //和当前本版比对，如果不一样，直接重置配置文件
            tutuconfig_delall_file();
            tutuconfig_load_default();
            tutuconfig_save();

            //如果是升级的新版本，则保存新的版本号进去
            tutuconfig_save_versioncode();
        }

        //如果能读取到配置文件中的版本号，则加载配置文件
        tutuconfig_load();
    }else{
        //第一次启动，则会把配置文件写入设备
        tutuconfig_load_default();
        tutuconfig_save();
        tutuconfig_save_versioncode();
    }    
    tutuconfig_load_servercfg();
}